﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Collections.Generic;
using UIDesigner.Designers;
using SharedClasses;
using System.ComponentModel.Design;
using DDW;
using AFSharedLibrary;
using UIDesigner.FieldHandlers;
using System.IO;
using System.Reflection;
using UIDesigner.FieldHandlers.Bases;
using UIDesigner.DesignContainers.Bases;
using UIDesigner.Editors;

namespace UIDesigner.DesignContainers
{
    [Designer(typeof(Design.ObjectDataGridDesigner))]
    partial class ObjectDataGrid : BaseObjectControl
    {
        private Construct m_objConstruct;
        private string m_strObjectName;
        private string m_strHeader;
        private Dictionary<string, FieldSpecification> m_rgFieldSpecifications = new Dictionary<string, FieldSpecification>();
        private Label m_lblHeader;

        public override void HandleSurfaceProperties(DynamicEnumType objDynamicEnumType)
        {
            throw new NotImplementedException();
        }

        [Browsable(false)]
        public override Construct BusinessObject
        {
            get
            {
                return m_objConstruct;
            }
        }

        [Browsable(false)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                Rebuild();
            }
        }

        [Editor(typeof(FieldSpecEditor), typeof(UITypeEditor))]
        [Category("Data"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public Dictionary<string, FieldSpecification> FieldSpecifications
        {
            get 
            { 
                return m_rgFieldSpecifications; 
            }
        } 

        [Category("Appearance")]
        public string HeaderText
        {
            get 
            { 
                return m_strHeader; 
            }
            
            set 
            { 
                m_strHeader = value;
            }
        }

        public string ObjectName
        {
            get 
            { 
                return m_strObjectName; 
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Construct Construct
        {
            set 
            { 
                m_objConstruct = value;
                m_strObjectName = m_objConstruct.Name;
                m_strHeader = m_objConstruct.Name; 
            }

            get
            {
                return m_objConstruct;
            }
        }

        public ObjectDataGrid()
        {
            this.Paint += new PaintEventHandler(OnPaint);
        }

        public override void OnFlush()
        {

        }

        protected override void OnLoad(object sender)
        {
            Rebuild();
        }

        private void Rebuild()
        {
            ClassNode objClassNode;
            InterfaceNode objInterfaceNode;

            this.Controls.Clear();

            if (m_strHeader != null && m_strHeader.Length > 0)
            {
                m_lblHeader = (Label)m_objHost.CreateComponent(typeof(Label));

                m_lblHeader.Visible = true;

                m_lblHeader.Height = 20;
                m_lblHeader.Dock = DockStyle.Top;
                m_lblHeader.BackColor = Color.Navy;
                m_lblHeader.ForeColor = Color.White;
                m_lblHeader.Text = m_strHeader;
                m_lblHeader.TextAlign = ContentAlignment.MiddleCenter;

                this.Controls.Add(m_lblHeader);
            }

            BaseFieldHandler.RequestType += new Handlers.RequestTypeHandler(BaseFieldHandler_RequestType);

            objClassNode = m_objConstruct.Node as ClassNode;

            Application.DoEvents();

            if (objClassNode != null)
            {
                foreach (PropertyNode objProperty in objClassNode.Properties)
                {
                    if (IsBrowsable(objProperty) && !IsBusObjectMember(objProperty))
                    {
                        SortedDictionary<float, IFieldHandler> rgHandlers;
                        FieldSpecification objSpec;

                        StatusBar.Text = "Adding default controls for \"" + objProperty.Names[0].GenericIdentifier + "\"";
                        Application.DoEvents();

                        rgHandlers = BaseFieldHandler.FindHandlers(this, objProperty, m_objHost);

                        if (rgHandlers.Count > 0)
                        {
                            objSpec = new FieldSpecification(this, objClassNode, objProperty, rgHandlers);

                            m_rgFieldSpecifications.Add(objSpec.MemberName, objSpec);
                        }
                    }
                }
            }
            else
            {
                objInterfaceNode = m_objConstruct.Node as InterfaceNode;

                if (objInterfaceNode != null)
                {
                    foreach (InterfacePropertyNode objProperty in objInterfaceNode.Properties)
                    {
                        if (IsBrowsable(objProperty) && !IsBusObjectMember(objProperty))
                        {
                            SortedDictionary<float, IFieldHandler> rgHandlers = BaseFieldHandler.FindHandlers(this, objProperty, m_objHost);
                            FieldSpecification objSpec = new FieldSpecification(this, objInterfaceNode, objProperty, rgHandlers);

                            StatusBar.Text = "Adding default controls for \"" + objProperty.Names[0].GenericIdentifier + "\"";
                            Application.DoEvents();

                            m_rgFieldSpecifications.Add(objSpec.MemberName, objSpec);
                        }
                    }
                }
            }

            StatusBar.Text = "Ready";
        }
    }
}
